// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "客户姓名",
    "title": "姓名"
  },
  "phone": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "联系电话",
    "title": "手机号"
  },
  "id_card": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "label": "身份证",
    "title": "身份证号"
  },
  "type": {
    "rules": [
      {
        "required": true
      }
    ],
    "label": "服务类型",
    "title": "业务类型"
  },
  "status": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "label": "当前状态",
    "title": "客户状态"
  },
  "source": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "客户来源",
    "label": "客户来源"
  },
  "debt_info": {
    "title": "负债情况",
    "label": "负债情况"
  },
  "repayment_date": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "还款日",
    "label": "还款日"
  },
  "birthday": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "生日",
    "label": "生日"
  },
  "associated_users": {
	  "rules": [
	    {
	      "format": "string"
	    }
	  ],
    "title": "关联人员",
    "label": "关联人员"
  },
  "department_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "所属部门",
    "label": "所属部门"
  },
  "is_public": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "公海池状态",
    "defaultValue": false,
    "label": "公海池状态"
  },
  "spouse": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "配偶信息",
    "label": "配偶信息"
  },
  "company_info": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "企业信息",
    "label": "企业信息"
  }
}

const enumConverter = {
	"type_valuetotext": {
	  "credit": "信贷",
	  "mortgage": "抵押",
	  "cash": "现金业务",
	  "company":"企业融资"
	},
  "status_valuetotext": {
    "potential": "潜在",
    "to_follow": "待跟进",
    "intention": "意向",
    "visit": "上门",
    "deal": "成交"
  },
  "source_valuetotext": {
    "online": "线上",
    "channel": "渠道",
    "tel_sales": "电话",
    "wechat": "微信",
    "referral": "转介绍"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
